#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : Book.py
# @Author: Xuhao
# @Date  : 2018/3/16
# @Desc  : 

from flask_restful import Resource
from flask import request
from flask_restful import reqparse, abort
from app.Modules.book_modules import NoverInfo, BookSource
from app.Modules.booktj_modules import BookJinDian,BookBoyTuijian,BookladyTuijian, BooklianzaiTuijian
from flask import jsonify
import json

class BookPinDaoTuijian(Resource):
    def get(self):
        jsondata = request.args.to_dict()

        # 类型索引
        index = jsondata['index']

        if int(index) == 0: #男频

            # 0. 火热推荐， 1. 连载热推， 2.新书看点
            pass

        else: #女频
            pass
            # 0. 火热推荐， 1. 连载热推， 2.新书看点

# 书城推荐接口
class BookTuijian(Resource):
    def get(self):

        jsondata = request.args.to_dict()

        # 类型索引
        index = jsondata['index']
        fl = jsondata['fl'] # 0 全部，男生 = 1， 女生 = 2，
        ret = None

        if int(fl) == 0:
            # 经典, 男生， 女神
            if int(index) == -1:

                jd = BookJinDian.select_JinDian()
                ns = BookBoyTuijian.select_BoyJinDian()
                nv = BookladyTuijian.select_ladyJinDian()

                ret = [{'index': 0,
                        'ismore': 1,
                        'title': '经典推荐',
                        'data': json.loads(jd)},
                       {'index': 1,
                        'ismore': 1,
                        'title': '男生最爱',
                        'data': json.loads(ns)},
                       {'index': 2,
                        'ismore': 0,
                        'title': '女生最爱',
                        'data': json.loads(nv)}
                       ]
            elif int(index) == 0:

                jd = BookJinDian.select_JinDian()

                ret = [{'index':0,
                         'title': '经典推荐',
                         'data':json.loads(jd)}
                        ]

            elif int(index) == 1:
                # 获取 经典 = 0，男生 = 1， 女生 = 2，

                ns = BookBoyTuijian.select_BoyJinDian()

                ret = [{'index': 1,
                        'title': '男生最爱',
                        'data': json.loads(ns)}]
            elif int(index) == 2:
                nv = BookladyTuijian.select_ladyJinDian()

                ret = [{'index': 2,
                        'title': '女生最爱',
                        'data': json.loads(nv)}]


            if ret != None:
                return jsonify({'error_code': 0,
                        'data': ret})
            else:
                return {'error_code': 1001,
                        'data': ret}
        elif int(fl) == 1 or int(fl) == 2:

            #男频，女频  包括， 经典, 连载
            if int(index) == -1:
                # 全部, 经典， 连载
                jd = BookJinDian.select_JinDian(fl=int(fl))
                lz = BooklianzaiTuijian.select_lianzai(fl=int(fl))


                ret = [{'index': 0,
                        'title': '火热推荐',
                        'ismore': 1,
                        'data': json.loads(jd)},
                       {'index': 1,
                        'title': '连载推荐',
                        'ismore': 0,
                        'data': json.loads(lz)}
                       ]
            elif int(index) == 0:
                # 经典
                jd = BookJinDian.select_JinDian(fl=int(fl))
                ret = [{'index': 0,
                        'title': '火热推荐',
                        'data': json.loads(jd)}
                       ]
            elif int(index) == 1:
                # 连载
                jd = BooklianzaiTuijian.select_lianzai(fl=int(fl))
                ret = [{'index': 0,
                        'title': '连载推荐',
                        'data': json.loads(jd)}
                       ]

            if ret != None:
                return jsonify({'error_code': 0,
                        'data': ret})
            else:
                return {'error_code': 1001,
                        'data': ret}




# Book
# 根据bookID获取书籍详情
class BookDetail(Resource):
    def get(self):

        jsondata = request.args.to_dict()

        ret = NoverInfo.fetchBookDetai(jsondata['bookID'])

        if ret != None:
            return jsonify({'error_code': 0,
                    'data': json.loads(ret)})
        else:
            return {'error_code': 1001,
                    'data': ret}




# BookList
# 根据 fl, StartID, count 获取小说列表
class BookList(Resource):
    def get(self):
        jsondata = request.args.to_dict()
        fl = jsondata['fl']
        startID = jsondata['startID']
        count = jsondata['count']

        ret = NoverInfo.select_booksWithRank(bookfl=fl, startID=startID, count=count)

        if ret != None:
            return jsonify({'error_code': 0,
                            'data': json.loads(ret),
                            'message': '获取列表成功'})
        else:
            return {'error_code': 1001,
                    'data': ret,
                    'message': '获取列表失败'}


# BookList
# 根据 fl, StartID, count 获取小说列表
class BookTypeList(Resource):
    def get(self):
        jsondata = request.args.to_dict()
        fl = jsondata['fl']
        startID = jsondata['startID']
        count = jsondata['count']

        ret = NoverInfo.select_booksWithGoddess(type=int(fl), startID=startID, count=count)

        if ret != None:
            return jsonify({'error_code': 0,
                            'data': json.loads(ret),
                            'message': '获取列表成功'})
        else:
            return {'error_code': 1001,
                    'data': ret,
                    'message': '获取列表失败'}


class BookSearching(Resource):
    '''
    查找小说
    '''
    def get(self):
        jsondata = request.args.to_dict()
        bookname = jsondata['bookName']

        ret = NoverInfo.select_book(bookName=bookname)

        if ret != None:
            return jsonify({'error_code': 0,
                    'data': json.loads(ret)})
        else:
            return {'error_code': 1001,
                    'data': ret}

class BookSources(Resource):
    '''
    根据BOOKID 获取URLS
    '''
    def get(self):

        jsondata = request.args.to_dict()
        bookID = jsondata['bookID']

        ret = BookSource.fetchBookSource(bookID=bookID)

        if ret != None:
            return jsonify({'error_code': 0,
                    'data': json.loads(ret)})
        else:
            return {'error_code': 1001,
                    'data': ret}


class BookRecommend(Resource):
    '''
    每日推荐 3本小说
    '''
    def get(self):

        jsondata = request.args.to_dict()
        count = jsondata['count']

        ret = NoverInfo.select_bookToday(count=int(count))

        if ret != None:
            return jsonify({'error_code': 0,
                    'data': json.loads(ret)})
        else:
            return {'error_code': 1001,
                    'data': ret}